<?php

    $s_orders_query = tep_db_query("select orders_id from " . TABLE_ORDERS . " where customers_id = '" . (int)$customer_id . "' order by date_purchased desc limit 1");
    $s_orders = tep_db_fetch_array($s_orders_query);
    $s_order_id = $s_orders['orders_id'];

    // Select Payment Method from orders table
    $s_payment_query = tep_db_query("select payment_method from " . TABLE_ORDERS . " where orders_id = '" . $s_order_id . "' AND customers_id = '" . (int)$customer_id . "'");
    $s_payment = tep_db_fetch_array($s_payment_query);
    $s_payment_method = $s_payment['payment_method'];

    // Path to setcom language file
    $s_filename = DIR_WS_LANGUAGES . $language . '/' . 'modules/payment/setcom.php';


    // If Payment Method used is setcom, then perform update
if (file_exists($s_filename)) {
    include($s_filename);
    if ($s_payment_method == MODULE_PAYMENT_SETCOM_TEXT_TITLE) {
    // Select Setcom ID (lastest purchase by current customer) from setcom table
    $s_setcom_query = tep_db_query("select s_setcom_id from " . TABLE_SETCOM . " where s_customers_id = '" . (int)$customer_id . "' order by s_setcom_id desc limit 1");
    $s_setcom = tep_db_fetch_array($s_setcom_query);
    $s_setcom_id = $s_setcom['s_setcom_id'];
    // Update setcom table with order no.
    tep_db_query("update " . TABLE_SETCOM . " set s_order_id = " . $s_order_id . " where s_setcom_id = " . $s_setcom_id);    
    }   
} else {
    if ($s_payment_method == 'Credit Card (Setcom)') {
    // Select Setcom ID (lastest purchase by current customer) from setcom table
    $s_setcom_query = tep_db_query("select s_setcom_id from " . TABLE_SETCOM . " where s_customers_id = '" . (int)$customer_id . "' order by s_setcom_id desc limit 1");
    $s_setcom = tep_db_fetch_array($s_setcom_query);
    $s_setcom_id = $s_setcom['s_setcom_id'];
    // Update setcom table with order no.
    tep_db_query("update " . TABLE_SETCOM . " set s_order_id = " . $s_order_id . " where s_setcom_id = " . $s_setcom_id);    
    }
  }
?>